package com.xyz.bigcorn.admin.common.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.xyz.bigcorn.admin.system.entity.User;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 * @Description: 注入公共字段自动填充,任选注入方式即可
 * @author: Zhaopengfei
 * @copyright: 金鹏信息技术有限公司
 * @CreatedDate: 2018年09月07日 14:21
 * @Copyright: All rights Reserved，Designed By Jpsycn
 */
public class MyMetaObjectHandler implements MetaObjectHandler {
    protected final static Logger logger = LoggerFactory.getLogger(MyMetaObjectHandler.class);


    @Override
    public void insertFill(MetaObject metaObject) {
        logger.info("新增的时候做操作");
        // 更多查看源码测试用例
        System.out.println("insert fill");
        //获取当前登录用户
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        setFieldValByName("createBy", user.getId(), metaObject);
        setFieldValByName("createName", user.getRealName(), metaObject);
        setFieldValByName("createDate", new Date(), metaObject);
        setFieldValByName("isDel", 0, metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        logger.info("更新的时候干点不可描述的事情");
        //更新填充
        System.out.println("*************************");
        System.out.println("update fill");
        System.out.println("*************************");
        //mybatis-plus版本2.0.9+
        //获取当前登录用户
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        setFieldValByName("updateBy", user.getId(), metaObject);
        setFieldValByName("updateName", user.getRealName(), metaObject);
        setFieldValByName("updateDate", new Date(), metaObject);

    }
}
